<?php
/**
 * Created by PhpStorm.
 * User: River
 * Email: river0428@gmail.com
 * Date: 2018/6/8
 * Time: 下午6:10
 */
namespace Modules\Attachment\Storage;

use JohnLui\AliyunOSS;

class OssStorage implements StorageInterface
{
    /* 城市名称：
     *
     *  经典网络下可选：杭州、上海、青岛、北京、张家口、深圳、香港、硅谷、弗吉尼亚、新加坡、悉尼、日本、法兰克福、迪拜
     *  VPC 网络下可选：杭州、上海、青岛、北京、张家口、深圳、硅谷、弗吉尼亚、新加坡、悉尼、日本、法兰克福、迪拜
     */
    private $city = '杭州';
    // 经典网络 or VPC
    private $networkType = '经典网络';

    private $AccessKeyId     = '';
    private $AccessKeySecret = '';
    private $ossClient;
    private $bucketName      = '';
    private $isInternal      = false;
    protected $ossDomain     = '';
    
    public function __construct($options)
    {
        $this->AccessKeyId     = $options['AccessKeyId'];
        $this->AccessKeySecret = $options['AccessKeySecret'];
        $this->bucketName      = $options['ossBucket'];
        $this->isInternal      = $options['isInternal'];
        $this->ossDomain      = $options['ossDomain'];

        $this->ossClient = AliyunOSS::boot(
            $this->city,
            $this->networkType,
            $this->isInternal,
            $this->AccessKeyId,
            $this->AccessKeySecret
        );
    }

    /**
     * 使用外网上传文件
     *
     * @param  string -上传之后的 OSS object 名称
     * @param  string -上传文件路径
     * @param  array
     *
     * @return mixed
     */
    public function upload($key, $filePath, $options = [])
    {
        $this->ossClient->setBucket($this->bucketName);
        $this->ossClient->uploadFile($key, $filePath, $options);
        return $this->ossDomain . $key;
    }

    /**
     * 使用外网直接上传变量内容
     *
     * @param  string -上传之后的 OSS object 名称
     * @param  string -上传的变量
     * @param  array
     *
     * @return mixed
     */
    public function uploadContent($key, $data, $options = [])
    {
        $this->ossClient->setBucket($this->bucketName);
        $this->ossClient->uploadContent($key, $data, $options);
        return $this->ossDomain . $key;
    }
    
    public function destroy($key)
    {
        $this->publicDeleteObject($key);
    }

    /**
     * 使用阿里云内网上传文件
     *
     * @param  string -上传之后的 OSS object 名称
     * @param  string -上传文件路径
     * @param  array
     *
     * @return mixed
     */
    public function privateUpload($ossKey, $filePath, $options = [])
    {
        $this->ossClient->setBucket($this->bucketName);
        return $this->ossClient->uploadFile($ossKey, $filePath, $options);
    }

    /**
     * 使用阿里云内网直接上传变量内容
     *
     * @param  string -上传之后的 OSS object 名称
     * @param  string -上传的变量
     *
     * @return mixed
     */
    public function privateUploadContent($ossKey, $content, $options = [])
    {
        $this->ossClient->setBucket($this->bucketName);
        return $this->ossClient->uploadContent($ossKey, $content, $options);
    }

    /**
     * 使用外网删除文件
     *
     * @param  string -目标 OSS object 名称
     *
     * @return mixed
     */
    public function publicDeleteObject($ossKey)
    {
        $this->ossClient->setBucket($this->bucketName);
        $this->ossClient->deleteObject($this->bucketName, $ossKey);
    }

    /**
     * 使用阿里云内网删除文件
     *
     * @param  string -目标 OSS object 名称
     *
     */
    public function privateDeleteObject($ossKey)
    {
        $this->ossClient->setBucket($this->bucketName);
        $this->ossClient->deleteObject($this->bucketName, $ossKey);
    }
}